<?php
/**
 * @author ykm 2015-12-15
 * @describe 电子面单请求类
 */
class erpapi_logistics_request_electron extends erpapi_logistics_request_abstract
{
    protected $title;
    protected $timeOut = 10;
    protected $primaryBn = '';
    protected $cacheLimit = 5000;
    protected $directNum = 1;
    protected $everyNum = 100;

    //电子面单请求统一出口
    final protected function requestCall($method,$params,$callback=array())
    {
        if(!$this->title) {
            $this->title = $this->__channelObj->channel['name'] . $this->__channelObj->channel['channel_type'] . '获取电子面单';
        }
        $this->__caller->writeFailLog = false;
        
        return $this->__caller->call($method,$params,$callback,$this->title, $this->timeOut, $this->primaryBn);
    }

    protected function bufferBackToRet($rlt) {//各自实现
        return array();
    }

    //缓存池异步回调方法
    public function bufferRequestCallBack($result, $callback_params){
        $rlt = $this->callback($result, $callback_params);
        $arrWaybill = $this->bufferBackToRet($rlt);
        if(empty($arrWaybill)) {
            return array('rsp'=>'fail', 'msg'=>'数据处理失败');
        }
        $objChannel = app::get('logisticsmanager')->model('channel');
        $channel = $objChannel->dump(array('channel_id'=>$callback_params['channel_id'], 'status'=>'true'));
        if(empty($channel)) {
            return array('rsp'=>'fail', 'msg'=>'电子面单来源缺失或停用');
        }
        $waybillModel = app::get('logisticsmanager')->model('waybill');
        $data = array();
        foreach($arrWaybill as $val) {
            $val = trim($val);
            $row = $waybillModel->dump(array('channel_id'=>$channel['channel_id'], 'waybill_number'=>$val), 'id');
            if(!$row) {
                $data[] = array(
                    'waybill_number' => $val,
                    'channel_id' => $channel['channel_id'],
                    'logistics_code' => $channel['logistics_code'],
                    'status' => 0,
                    'create_time' => time(),
                );
            }
        }
        if(empty($data)) {
            return array('rsp'=>'fail', 'msg'=>'没有可用的单号');
        }
        $insertSql = ome_func::get_insert_sql($waybillModel, $data);
        $ret = $waybillModel->db->exec($insertSql);
        if($ret) {
            return array('rsp'=>'succ', 'msg'=>'数据写入成功');
        } else {
            return array('rsp'=>'fail', 'msg'=>'数据写入失败');
        }
    }

    //回传物流公司请求统一接口
    final protected function deliveryCall($method,$logData,$params){
        if(!$this->title) {
            $this->title = $this->__channelObj->channel['name'] . $this->__channelObj->channel['channel_type'] . '电子面单物流回传';
        }
        $this->__caller->writeFailLog = false;

        $logData['obj_bn'] = $this->primaryBn;
        $callback = array(
            'class' => get_class($this),
            'method' => 'deliveryBack',
            'params' => $logData
        );
        
        $ret = $this->__caller->call($method, $params, $callback, $this->title, $this->timeOut, $this->primaryBn);
        $this->logisticsLog($logData['logi_no'], $logData['delivery_id'], $params);
        
        return true;
        
        
    }

    public function deliveryBack($result, $callback_params) {

        if (in_array($callback_params['channel_type'],array('ems','360buy'))){
            $rlt = $this->callback($result, $callback_params);
            
            $logisticsLogObj = app::get('logisticsmanager')->model('logistics_log');
            if ($rlt['rsp'] == 'succ'){
                $logisticsLogObj->update(array('status'=>'success'),array('delivery_id'=>$callback_params['delivery_id'],'logi_no'=>$callback_params['logi_no']));
            } else {
                $logisticsLogObj->update(array('status'=>'fail'),array('delivery_id'=>$callback_params['delivery_id'],'logi_no'=>$callback_params['logi_no']));
            }
        }
        
        return true;
    }

    protected function logisticsLog($logiNo, $delivery_id, $params) {
        if (in_array($this->__channelObj->channel['channel_type'],array('ems','360buy'))){
            //回填日志只针对EMS 和京东记录便于重试
            $logisticsLogObj = &app::get('logisticsmanager')->model('logistics_log');
            $row = $logisticsLogObj->dump(array('logi_no'=>$logiNo, 'delivery_id'=>$delivery_id), 'log_id, retry');
            if($row['log_id']) {
                return $logisticsLogObj->update(array('retry'=>$row['retry']+1), array('log_id'=>$row['log_id']));
            }
            $logSdf = array(
                'logi_no' => $logiNo,
                'delivery_id' => $delivery_id,
                'channel_id' => $this->__channelObj->channel['channel_id'],
                'channel_type'=>$this->__channelObj->channel['channel_type'],
                'status' => 'running',
                'create_time' => time(),
                'params' => $params,
            );
            return $logisticsLogObj->insert($logSdf);
        }else{
            return true;
        }
        
    }

    //是否直辖市
    public function isMunicipality($province) {
        $municipality = array('北京市', '上海市', '天津市', '重庆市');
        $status = false;
        foreach ($municipality as $zxs) {
            if (substr($zxs, 0, strlen($province)) == $province) {
                $status = true;
                break;
            }
        }
        return $status;
    }

    #过滤特殊字符
    public function charFilter($str){
        return str_replace(array("<",">","&","'",'"',''),'',$str);
    }

    /**
    * 处理直连返回结果
    *
    */
    public function directDataProcess($data){
        $channel = $this->__channelObj->channel;

        $objWaybill = app::get('logisticsmanager')->model('waybill');
        $waybillExtendModel = app::get('logisticsmanager')->model('waybill_extend');
        foreach ($data as $val){

            if($val['succ']) {
                $logi_no = trim($val['logi_no']);
                $arrWaybill = $objWaybill->dump(array('channel_id' => $channel['channel_id'], 'waybill_number' => $logi_no),'id,status');
                if (!$arrWaybill) {
                    $arrWaybill = array(
                        'waybill_number' => $logi_no,
                        'channel_id'     => $channel['channel_id'],
                        'logistics_code' => $channel['logistics_code'],
                        'status'         => 1,
                        'create_time'    => time(),
                    );
                    $ret = $objWaybill->insert($arrWaybill);
                
                } elseif ($arrWaybill['status'] == '2') {
                    $objWaybill->update(array('status'=>'1'),array('id'=>$arrWaybill['id']));
                } 
                if(!$val['noWayBillExtend']) {
                    $waybillExtend = array(
                        'waybill_id' => $arrWaybill['id'],
                        'mailno_barcode' => $val['mailno_barcode'],
                        'qrcode' => $val['qrcode'],
                        'position' => $val['position'],
                        'position_no' => $val['position_no'],
                        'package_wdjc' => $val['package_wdjc'],
                        'package_wd' => $val['package_wd'],
                        'print_config' => $val['print_config'],
                        'json_packet' => $val['json_packet'],
                    );
                    
                    $filter = array('waybill_id' => $waybillExtend['waybill_id']);
                    if (!$waybillExtendModel->dump($filter)) {
                        $ret = $waybillExtendModel->insert($waybillExtend);
                    } else {
                        $ret = $waybillExtendModel->update($waybillExtend, $filter);
                    }
                    
                }
            }
        }
        
    }

    public function bind() {
        $params = array(
            'app' => 'app.applyNodeBind',
            'node_id' => base_shopnode::node_id('ome'),
            'from_certi_id' => base_certificate::certi_id(),
            'callback' => kernel::openapi_url('openapi.ome.shop','shop_callback',array('channel_type'=>$this->node_type)),
            'sess_callback' => urlencode(kernel::openapi_url('openapi.ome.shop','shop_callback',array('channel_type'=>$this->node_type))),
            'api_url' => kernel::base_url(1).kernel::url_prefix().'/api',
            'node_type' => $this->node_type,
            'to_node' => $this->to_node,
            'shop_name' => $this->shop_name,
        );
        $token = base_certificate::token();
        $params['certi_ac'] = $this->genBindSign($params,$token);
        //$api_url = 'http://sws.ex-sandbox.com/api.php';
        $api_url = 'http://www.matrix.ecos.shopex.cn/api.php';
        $headers = array(
            'Connection' => 5,
        );
        
        $core_http = kernel::single('base_httpclient');
        $response = $core_http->set_timeout(5)->post($api_url, $params, $headers);
        
        $response = json_decode($response,true);
       
        $status = false;
        if($response['res']=='succ' || $response['msg']['errorDescription'] == '绑定关系已存在,不需要重复绑定') {
            $status = true;
        }
        return $status;
    }

    public function genBindSign($params, $token) {
        ksort($params);
        $str = '';
        foreach ($params as $key =>$value) {
            if ($key != 'certi_ac') {
                $str .= $value;
            }
        }
        $signString = md5($str.$token);
        return $signString;
    }
    

    protected function _formate_receiver_city($receiver_city)
    {
        $zhixiashi = array('北京','上海','天津','重庆');
        $zizhiqu = array('内蒙古','宁夏回族','新疆维吾尔','西藏','广西壮族');

        if (in_array($receiver_city,$zhixiashi)) {
           $receiver_city = $receiver_city.'市';
        }else if (in_array($receiver_city,$zizhiqu)) {
            $receiver_city = $receiver_city.'自治区';
        }else if($receiver_city == '广西'){
            $receiver_city = $receiver_city.'壮族自治区';
        }else if($receiver_city == '宁夏'){
            $receiver_city = $receiver_city.'回族自治区';

        }else if($receiver_city == '新疆'){
            $receiver_city = $receiver_city.'维吾尔自治区';
        }

        elseif(!preg_match('/(.*?)省/',$receiver_city)){
            $receiver_city = $receiver_city.'省';
        }
        return $receiver_city;
    }
}
